package org.example.demo0913;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.sql.Driver;
import java.sql.DriverManager;
import java.io.IOException;
import java.sql.*;
import java.util.Scanner;
@SpringBootApplication
public class Demo0913Application {

    public static void main(String[] args) {


        class Main {
            public static void main(String[] args) throws Exception {
                m1();
                m2();
                m3();

            }

            private static void m3 () throws Exception{
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8","root","123456");
                String sql="insert into sys_user values(?,?,?)";
                PreparedStatement ps=conn.prepareStatement(sql);
                ps.setString(1,"3");
                ps.setString(2,"admin");
                ps.setString(3,"admin");
                int i = ps.executeUpdate();
                ps.close();
                conn.close();
            }

            private static void m1() throws Exception {
                //1.加载驱动
                Class.forName("com.mysql.cj.jdbc.Driver");
                //2.获取连接
                Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8", "root", "123456");
                //3.编写sql语句
                String sql="select * from sys_user";
                //4.预处理sql var.
                PreparedStatement preparedStatement = conn.prepareStatement(sql);
                //5.如果有参数，设置参数
                //6.执行sql
                ResultSet resultSet = preparedStatement.executeQuery();
                //7.如果是查询就遍历结果，如果是增删改查 判断是否成功
                while (resultSet.next()) {
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    String pass = resultSet.getString(3);
                    System.out.println("id:"+id+",name:"+name+",pass:"+pass);
                }
                //8.关闭资源
                resultSet.close();
                preparedStatement.close();
                conn.close();
            }

            private static void m2() throws Exception{
                Connection conn=null;
                PreparedStatement preparedStatement=null;
                ResultSet resultSet=null;
                try {
                    Scanner sc = new Scanner(System.in);
                    System.out.println("请输入您要输入的字符");
                    String word = sc.next();
                    //1.加载驱动
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    //2.获取连接
                    conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8", "root", "123456");
                    //3.编写sql语句
                    String sql = "select * from sys_user where name like ?";
                    //4.预处理sql var.
                    preparedStatement = conn.prepareStatement(sql);
                    //5.如果有参数，设置参数
                    preparedStatement.setString(1, "zhangsan");
                    //6.执行sql
                    resultSet = preparedStatement.executeQuery();
                    //7.如果是查询就遍历结果，如果是增删改查 判断是否成功
                    while (resultSet.next()) {
                        int id = resultSet.getInt(1);
                        String name = resultSet.getString(2);
                        String pass = resultSet.getString(3);
                        System.out.println("id:" + id + ",name:" + name + ",pass:" + pass);
                    }
                }
                catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
                catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                finally {
                    //8.关闭资源
                    try{
                        if(resultSet !=null){
                        resultSet.close();}
                        if(preparedStatement !=null){
                    preparedStatement.close();}
                        if(conn !=null){
                    conn.close();}}catch(SQLException e){
                        throw new RuntimeException(e);
                    }
                }
            }
        }

    }

}
